package edu.tgc.entity.file;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Embeddable;

import org.springframework.util.StringUtils;

@Embeddable
public class FilePath {

    protected String host = "127.0.0.1";
    protected String password;
    protected String path;
    protected Integer port;
    protected String protocol = "file";
    protected String username;

    public FilePath() {
    }

    public FilePath(String path) {
        this.path = path;
    }

    public FilePath(String protocol, String host, Integer port, String path, String username, String password) {
        this.protocol = protocol;
        this.host = host;
        this.port = port;
        this.path = path;
        this.username = username;
        this.password = password;
    }

    @Column(length = 255)
    public String getHost() {
        return this.host;
    }

    @Column(length = 255)
    public String getPassword() {
        return this.password;
    }

    @Column(length = 255)
    public String getPath() {
        return this.path;
    }

    public Integer getPort() {
        return this.port;
    }

    @Column(length = 15)
    public String getProtocol() {
        return this.protocol;
    }

    @Column(length = 255)
    public String getUsername() {
        return this.username;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String toString() {
        String serverPath = this.protocol + "://" + this.host + (this.port == null ? "" : ":" + this.port.toString());
        List<String> parameters = new ArrayList<>();
        if (this.username != null) {
            parameters.add("username=" + this.username);
        }
        if (this.password != null) {
            parameters.add("password=" + this.password);
        }
        if (parameters.size() > 0) {
            serverPath += "?" + StringUtils.collectionToDelimitedString(parameters, "&");
        }
        return serverPath;
    }
}
